/**
   * httpClient请求拦截器
   */
  define('httpClientRequestInterceptor',[], function () {
    var HttpClientRequestInterceptor=function(requestHandle,errorHandle){
      if(typeof requestHandle === 'function'){
        this.requestHandle=requestHandle;
      }
      if(typeof errorHandle === 'function'){
        this.errorHandle=errorHandle;
      }

    }
    HttpClientRequestInterceptor.prototype.injectInterceptor=function(axiosInstance,options){
      // 添加一个请求拦截器
      axiosInstance.interceptors.request.use((config)=>{
        return this.requestHandle(config,options);
      },(error)=>{
        return this.errorHandle(error,options);
      });
    }
    return HttpClientRequestInterceptor;
  }),
  /**
   * httpClient响应拦截器
   */
  define('httpClientResponseInterceptor',[], function () {
    var HttpClientResponseInterceptor=function(responseHandle,errorHandle){
      if(typeof responseHandle === 'function'){
        this.responseHandle=responseHandle;
      }
      if(typeof errorHandle === 'function'){
        this.errorHandle=errorHandle;
      }

    }
    HttpClientResponseInterceptor.prototype.injectInterceptor=function(axiosInstance,options){
      // 添加一个请求拦截器
      axiosInstance.interceptors.response.use((response)=>{
        return this.responseHandle(response,options);
      },(error)=>{
        return this.errorHandle(error,options);
      });
    }
    return HttpClientResponseInterceptor;
  })
  /**
   * httpClient
   */
  define('httpClient',['axios','httpClientRequestInterceptor','httpClientResponseInterceptor','qs'], function (Axios,HttpClientRequestInterceptor,HttpClientResponseInterceptor,qs) {
    var httpClientDefaultOptions={
      baseURL: 'http://localhost:8077/',
      timeout: 2000,
      headers: {
        'Content-Type': 'application/json; charset=utf-8'
      }
    };

    var HttpClient=function(options){
      this.options = Object.assign(httpClientDefaultOptions, options);
    }
    HttpClient.prototype.injectRequestInterceptor=function(axiosInstance,options){
      if(this.options.requestInterceptor && this.options.requestInterceptor instanceof HttpClientRequestInterceptor){
        this.options.requestInterceptor.injectInterceptor(axiosInstance,options)
      }
    }
    HttpClient.prototype.injectResponseInterceptor=function(axiosInstance,options){
      if(this.options.responseInterceptor && this.options.responseInterceptor instanceof HttpClientResponseInterceptor){
        this.options.responseInterceptor.injectInterceptor(axiosInstance,options)
      }
    }

      HttpClient.prototype.request=function(options){
        if(options){
            if(!options.method){
                options.method="get";
            }
            if(!options.withCredentials){
                options.withCredentials=true;
            }
            if(!options.params){
                options.params={};
            }
            if(!options.data){
                options.data={};
            }else {
                if(options.data instanceof Object){
                    options.data=JSON.stringify(options.data);
                }
            }
            if(!options.headers){
                options.headers=httpClientDefaultOptions.headers;
            }
            // if(!options.transformRequest){
            //     options.transformRequest=[];
            // }
        }
          //let opts=Object.assign(this.options, options);

          let axiosInstance=Axios.create(this.options);

          this.injectRequestInterceptor(axiosInstance,options);
          this.injectResponseInterceptor(axiosInstance,options);
          //return axiosInstance(options);
          return new Promise((resolve, reject) =>{
              axiosInstance(options).then(res=>{
                  if (!(res instanceof Blob)) {
                      if (res.code && res.code === 401) {
                          reject(res.error,res)
                          // 令牌无效，清楚本地保存的令牌
                          if(this.options.authorizingRealm){
                              authorizingRealm.unAuthorized(options);
                          }
                          return false
                      } else if (res.code && res.code !== 200) {
                          reject(res.error,res)
                          return false
                      }
                  }
                  resolve(res)
              }).catch(reason => {
                  reject(reason.message)
              })
          });
      }

    return HttpClient;
  })